package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.User;
import com.neuedu.yyzx.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 金山
 * 项目：yyzx
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/2514:23
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * http://localhost:8080/user/list
     *
     * @return
     */
    @GetMapping("/list")
    public Ret list(){


        return  Ret.ok(userService.list());
    }

    @GetMapping("/div/{numa}/{numb}")
    public Ret div(@PathVariable  int numa,@PathVariable int numb){


        // Ret ret =  new Ret();
        // ret.setCode(200);
        // ret.setData(numa / numb);
        // return ret;

        return  Ret.ok(numa / numb);
    }



    @GetMapping("/page")
    public Ret<List<User> > page(Page<User> page, User user){


        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if(user.getNickname() != null  && !"".equals(user.getNickname().trim())){
            queryWrapper.like(User::getNickname , user.getNickname());   //  where  nickname like  ?
        }

        // ORDER BY createtime DESC
        queryWrapper.orderByDesc(User::getCreateTime) ;


        page = userService.page(page,queryWrapper);
        return  Ret.ok(page);
    }


    /**
     * 用户的添加或者更细
     * @param user
     * @return
     */
    @Operation(summary = "用户保存或者更新")
    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody User user){
        //将浏览器的明文 转换成加密 的 秘闻
        String rawPassword = user.getPassword();

        //设置成秘闻
        user.setPassword( passwordEncoder.encode(rawPassword)    );
        System.out.println("user.getPassword() = " + user.getPassword());


        boolean success = userService.saveOrUpdate(user);
        return  Ret.ok(success);
    }


    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Ret<User> get  (@PathVariable int id){
        User user = userService.getById(id);
        return  Ret.ok(user);
    }


    /**
     * 根据主键删除
     *
     * /user/id
     * method delete
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Ret<Boolean> removeById (@PathVariable int id){
        return  Ret.ok(userService.removeById(id));
    }

}
